package com.cy.test;

import com.cy.mapper.UserMapper;
import com.cy.mapper.UserMapper2;
import com.cy.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

public class mybatisTest2 {
    InputStream stream = null;
    SqlSessionFactory build = null;
    SqlSession sqlSession = null;
    UserMapper2 mapper = null;
    @Before
    public void  testbefore() throws Exception {
        String s="mybatis-config.xml";
        stream = Resources.getResourceAsStream(s);
        build = new SqlSessionFactoryBuilder().build(stream);
        sqlSession = build.openSession();
        mapper = sqlSession.getMapper(UserMapper2.class);
    }

    @After
    public void tesafter(){
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test01(){
        List<User> users = mapper.selectFindAll();
        users.forEach(System.out::println);

    }

    @Test
    public void test02(){
        User userById = mapper.getUserById(4);
        System.out.println(userById);
    }

    @Test
    public void test03(){
//        int i = mapper.insertUser(new User(null, "王也", 20));
        User user = new User(null, "王也", 20);
        int i = mapper.insertUser(user);
    }

    @Test
    public void  test04(){
        int i = mapper.deleteUser(5);
        System.out.println(i);
        test01();
    }

    @Test
    public void test05(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("age",130);
        map.put("id",1);
        int i = mapper.updateUser2(map);
        System.out.println(i);
        test01();
    }

    @Test
    public void test06(){
        List<User> list = mapper.selectlist("张");
        list.forEach(System.out::println);
    }
}
